﻿import mx.xpath.XPathAPI;
class awsapi {
	
	private var apiURL:String = "http://webservices.amazon.com/onca/xml?Service=AWSECommerceService";
	private var SubscriptionId:String = "0525E2PQ81DD7ZTWTK82";
	private var AssociateTag:String ="apodnet";
	private var xmlRequest:XML;
	
	private var listenerobj:Object = null;
	
	///////////////////////////////////////////////////////
	/*                  response groups                  */
	///////////////////////////////////////////////////////
	public static var ACCESSORIES_RG:String = "Accessories";
	public static var ITEMIDS_RG:String = "ItemIds";
	public static var OFFERSUMMARY_RG:String = "OfferSummary";
	public static var SIMILARITIES_RG:String = "Similarities";
	public static var BROWSENODES_RG:String = "BrowseNodes";
	public static var LARGE_RG:String = "Large";
	public static var OFFERS_RG:String = "Offers";
	public static var SMALL_RG:String = "Small";
	public static var EDITORIALREVIEW_RG:String = "EditorialReview";
	public static var LISTMANIALISTS_RG:String = "ListmaniaLists";
	public static var REQUEST_RG:String = "Request";
	public static var TRACKS_RG:String = "Tracks";
	public static var IMAGES_RG:String = "Images";
	public static var MEDIUM_RG:String = "Medium";
	public static var REVIEWS_RG:String = "Reviews";
	public static var VARIATIONMINIMUM_RG:String = "VariationMinimum";
	public static var ITEMATTRIBUTES_RG:String = "ItemAttributes";
	public static var OFFERFULL_RG:String = "OfferFull";
	public static var SALESRANK_RG:String = "SalesRank";
	public static var VARIATIONSUMMARY_RG:String = "VariationSummary";
	public static var LISTINFO_RG:String = "ListInfo";
	public static var LISTMINIMUM_RG:String = "ListMinimum";
	
	///////////////////////////////////////////////////////
	/*                 search indexes                    */
	///////////////////////////////////////////////////////
	public static var BOOKS_SI = "Books";
	
		
	public function awsapi(listener:Object){
		listenerobj = listener;
		apiURL+="&SubscriptionId="+SubscriptionId;
		xmlRequest = new XML();
		xmlRequest.onLoad = mx.utils.Delegate.create(this,handleResponse);
		xmlRequest.ignoreWhite = true;
	}
	
	public function BrowseNodeLookup(){
	}
	
	public function CartAdd(){
	}
	
	public function CartCreate(){
	}
	
	public function CartGet(){
	}
	
	public function CartModify(){
	}
	
	public function CustomerContentLookup(){
	}
	
	public function CustomerContentSearch(){
	}
	
	public function Help(){
	}
	
	public function ItemLookup(requestParams:awsParamCollection){
		var requestURL:String = apiURL+"&Operation=ItemLookup";
		requestURL+=requestParams.toURL();
		xmlRequest.load(requestURL);
	}
	
	public function ItemSearch(requestParams:awsParamCollection){
		var requestURL:String = apiURL +"&Operation=ItemSearch";
		requestURL += requestParams.toURL();
		trace(requestURL);
		xmlRequest.load(requestURL);
	}
	
	public function ListLookup(requestParams:awsParamCollection){
		var requestURL:String = apiURL+"&Operation=ListLookup";
		requestURL+=requestParams.toURL();
		xmlRequest.load(requestURL);
	}
	
	public function ListSearch(requestParams:awsParamCollection){
		var requestURL:String = apiURL+"&Operation=ListSearch";
		requestURL+=requestParams.toURL();
		xmlRequest.load(requestURL);
	}
	
	public function SellerListingLookup(){
	}
	
	public function SellerListingSearch(){
	}
	
	public function SellerLookup(){
	}
	
	public function SimilarityLookup(){
	}
	
	public function TransactionLookup(){
	}
	
	private function handleResponse(){
		trace("Handling Response............");
		trace(xmlRequest);
		var response = xmlRequest.firstChild;
		var operationType = xmlRequest.firstChild.nodeName;
		var responseArray = new Array();
		trace("Checking for Errors");
		var error_array = XPathAPI.selectNodeList(response,"/*/*/Request/Errors/Error");
		var errorLen = error_array.length;
		if(errorLen > 0){
			trace("there were errors!");
			for(var i = 0; i < errorLen; i++){
				var Code =  XPathAPI.selectSingleNode(error_array[i],"/Error/Code").firstChild.nodeValue;
				var Message = XPathAPI.selectSingleNode(error_array[i],"/Error/Message").firstChild.nodeValue;
				responseArray.push({Code:Code,Message:Message});
			}
			listenerobj.errorHandler(responseArray,operationType);
			return;
		}
		var item_array = XPathAPI.selectNodeList(response, "/*/Items/Item");
		
		for (var i = 0; i<item_array.length; i++) {
		
			var ASIN = XPathAPI.selectSingleNode(item_array[i],"/Item/ASIN").firstChild.nodeValue;
			var DetailPageURL = XPathAPI.selectSingleNode(item_array[i],"/Item/DetailPageURL").firstChild.nodeValue;
		
			//item attributes
			var Author =  XPathAPI.selectSingleNode(item_array[i],"/Item/ItemAttributes/Author").firstChild.nodeValue;
			var ProductGroup = XPathAPI.selectSingleNode(item_array[i],"/Item/ItemAttributes/ProductGroup").firstChild.nodeValue;
			var Title = XPathAPI.selectSingleNode(item_array[i],"/Item/ItemAttributes/Title").firstChild.nodeValue;
		
			//list price
			var Amount = XPathAPI.selectSingleNode(item_array[i],"/Item/ItemAttributes/ListPrice/Amount").firstChild.nodeValue;
			var CurrencyCode = XPathAPI.selectSingleNode(item_array[i],"/Item/ItemAttributes/ListPrice/CurrencyCode").firstChild.nodeValue;
			var FormattedPrice = XPathAPI.selectSingleNode(item_array[i],"/Item/ItemAttributes/ListPrice/FormattedPrice").firstChild.nodeValue;
			var listPrice = {
				Amount:Amount,
				CurrencyCode:CurrencyCode,
				FormattedPrice:FormattedPrice
			};
			
			//Features
			var Features:Array = new Array();
			var featuresArray = XPathAPI.selectNodeList(item_array[i],"/Item/ItemAttributes/Feature");
			for(var i in featuresArray){
				Features.push(featuresArray[i].firstChild.nodeValue);
			}
			//Height
			var Height = XPathAPI.selectSingleNode(item_array[i],"/Item/ItemAttributes/Height").firstChild.nodeValue;
			//Length
			var Length = XPathAPI.selectSingleNode(item_array[i],"/Item/ItemAttributes/Length").firstChild.nodeValue;
			//NumberOfItems
			var NumberOfItems = XPathAPI.selectSingleNode(item_array[i],"/Item/ItemAttributes/NumberOfItems").firstChild.nodeValue;
			//UPC
			var UPC = XPathAPI.selectSingleNode(item_array[i],"/Item/ItemAttributes/UPC").firstChild.nodeValue;
			//ProductGroup
			var ProductGroup = XPathAPI.selectSingleNode(item_array[i],"/Item/ItemAttributes/ProductGroup").firstChild.nodeValue;
			//Weight
			var Weight = XPathAPI.selectSingleNode(item_array[i],"/Item/ItemAttributes/Weight").firstChild.nodeValue;
			//Width
			var Width = XPathAPI.selectSingleNode(item_array[i],"Item/ItemAttributes/Width").firstChild.nodeValue;
			
			var itemAttributes = {
				Author:Author,
				ProductGroup:ProductGroup,
				Title:Title,
				ListPrice:listPrice,
				Features:Features,
				Height:Height,
				Length:Length,
				NumberOfItems:NumberOfItems,
				UPC:UPC,
				ProductGroup:ProductGroup,
				Weight:Weight,
				Width:Width
			};
			
			//OfferSummary
		
			//LowestNewPrice
			//Amount
			var Amount = XPathAPI.selectSingleNode(item_array[i],"Item/OfferSummary/LowestNewPrice/Amount").firstChild.nodeValue;
			//CurrencyCode
			var CurrencyCode = XPathAPI.selectSingleNode(item_array[i],"Item/OfferSummary/LowestNewPrice/CurrencyCode").firstChild.nodeValue;
			//FormattedPrice
			var FormattedPrice = XPathAPI.selectSingleNode(item_array[i],"Item/OfferSummary/LowestNewPrice/FormattedPrice").firstChild.nodeValue;
			var LowestNewPrice = {
				Amount:Amount,
				CurrencyCode:CurrencyCode,
				FormattedPrice:FormattedPrice
			};
		
		
			//LowestUsedPrice
			//Amount
			var Amount = XPathAPI.selectSingleNode(item_array[i],"Item/OfferSummary/LowestUsedPrice/Amount").firstChild.nodeValue;
			//CurrencyCode
			var CurrencyCode = XPathAPI.selectSingleNode(item_array[i],"Item/OfferSummary/LowestUsedPrice/CurrencyCode").firstChild.nodeValue;
			//FormattedPrice
			var FormattedPrice = XPathAPI.selectSingleNode(item_array[i],"Item/OfferSummary/LowestUsedPrice/FormattedPrice").firstChild.nodeValue;
			var LowestUsedPrice = {
				Amount:Amount,
				CurrencyCode:CurrencyCode,
				FormattedPrice:FormattedPrice
			}
			//LowestRefurbishedPrice
			//Amount
			var Amount = XPathAPI.selectSingleNode(item_array[i],"Item/OfferSummary/LowestRefurbishedPrice/Amount").firstChild.nodeValue;
			//CurrencyCode
			var CurrencyCode = XPathAPI.selectSingleNode(item_array[i],"Item/OfferSummary/LowestRefurbishedPrice/CurrencyCode").firstChild.nodeValue;
			//FormattedPrice
			var FormattedPrice = XPathAPI.selectSingleNode(item_array[i],"Item/OfferSummary/LowestRefurbishedPrice/FormattedPrice").firstChild.nodeValue;
			var LowestRefurbishedPrice = {
				Amount:Amount,
				CurrencyCode:CurrencyCode,
				FormattedPrice:FormattedPrice
			}
		
			//LowestCollectiblePrice
			//Amount
			var Amount = XPathAPI.selectSingleNode(item_array[i],"Item/OfferSummary/LowestCollectiblePrice/Amount").firstChild.nodeValue;
			//CurrencyCode
			var CurrencyCode = XPathAPI.selectSingleNode(item_array[i],"Item/OfferSummary/LowestCollectiblePrice/CurrencyCode").firstChild.nodeValue;
			//FormattedPrice
			var FormattedPrice = XPathAPI.selectSingleNode(item_array[i],"Item/OfferSummary/LowestCollectiblePrice/FormattedPrice").firstChild.nodeValue;
			var LowestCollectiblePrice = {
				Amount:Amount,
				CurrencyCode:CurrencyCode,
				FormattedPrice:FormattedPrice
			}
		
			//TotalNew
			var TotalNew = XPathAPI.selectSingleNode(item_array[i],"Item/OfferSummary/TotalNew").firstChild.nodeValue;
			//TotalUsed
			var TotalUsed = XPathAPI.selectSingleNode(item_array[i],"Item/OfferSummary/TotalUsed").firstChild.nodeValue;
			//TotalCollectible
			var TotalCollectible = XPathAPI.selectSingleNode(item_array[i],"Item/OfferSummary/TotalCollectible").firstChild.nodeValue;
			//TotalRefurbished
			var TotalRefurbished = XPathAPI.selectSingleNode(item_array[i],"Item/OfferSummary/TotalRefurbished").firstChild.nodeValue;

			var OfferSummary = {
				LowestNewPrice:LowestNewPrice,
				LowestUsedPrice:LowestUsedPrice,
				LowestRefurbishedPrice:LowestRefurbishedPrice,
				LowestCollectiblePrice:LowestCollectiblePrice,
				TotalNew:TotalNew,
				TotalUsed:TotalUsed,
				TotalCollectible:TotalCollectible,
				TotalRefurbished:TotalRefurbished
			}
			
			//EditorialReviews
			var EditorialReviews:Array = new Array();
			var reviews = XPathAPI.selectNodeList(item_array[i],"/Item/EditorialReviews/EditorialReview");
			for(var j in reviews){
				var r = {
					Source:XPathAPI.selectSingleNode(reviews[j],"/EditorialReview/Source").firstChild.nodeValue,
					Content:XPathAPI.selectSingleNode(reviews[j],"/EditorialReview/Source").firstChild.nodeValue
				};
				EditorialReviews.push(r);
			}
			
			//images
			var siURL = XPathAPI.selectSingleNode(item_array[i],"/Item/SmallImage/URL").firstChild.nodeValue;
			var siHeight = XPathAPI.selectSingleNode(item_array[i],"/Item/SmallImage/Height").firstChild.nodeValue;
			var siWidth = XPathAPI.selectSingleNode(item_array[i],"/Item/SmallImage/Width").firstChild.nodeValue;
			var SmallImage = {
				URL:siURL,
				Height:siHeight,
				Width:siWidth
			};
			
			var miURL = XPathAPI.selectSingleNode(item_array[i],"/Item/MediumImage/URL").firstChild.nodeValue;
			var miHeight = XPathAPI.selectSingleNode(item_array[i],"/Item/MediumImage/Height").firstChild.nodeValue;
			var miWidth = XPathAPI.selectSingleNode(item_array[i],"/Item/MediumImage/Width").firstChild.nodeValue;
			var MediumImage = {
				URL:miURL,
				Height:miHeight,
				Width:miWidth
			};
			
			var liURL = XPathAPI.selectSingleNode(item_array[i],"/Item/LargeImage/URL").firstChild.nodeValue;
			var liHeight = XPathAPI.selectSingleNode(item_array[i],"/Item/LargeImage/Height").firstChild.nodeValue;
			var liWidth = XPathAPI.selectSingleNode(item_array[i],"/Item/LargeImage/Width").firstChild.nodeValue;
			var LargeImage = {
				URL:liURL,
				Height:liHeight,
				Width:liWidth
			};
			
			
			var itemObj = {
				ASIN:ASIN,
				DetailPageURL:DetailPageURL,
				ItemAttributes:itemAttributes,
				SmallImage:SmallImage,
				MediumImage:MediumImage,
				LargeImage:LargeImage,
				OfferSummary:OfferSummary,
				EditorialReviews:EditorialReviews

			};
			responseArray.push(itemObj);
		}
		
		switch ( operationType ) {
			case "ItemSearchResponse":
				listenerobj.ItemSearchResponse(responseArray);
				break;
			case "ItemLookupResponse":
				listenerobj.ItemLookupResponse(responseArray);
				
		}
	}
	
}

